//
//  SEGVisitorAccessCodeInfoCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/5/12.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGVisitorAccessCodeInfoCell.h"
#import "SEGVisitorAccessCodeModel.h"

@interface SEGVisitorAccessCodeInfoCell ()

/**
 二维码图片视图
 */
@property (nonatomic, strong)UIImageView *qcodeImageView;

/**
 来访日期视图
 */
@property (nonatomic, strong)UILabel *visitDateLabel;

/**
 有效日期视图
 */
@property (nonatomic, strong)UILabel *validDateLabel;

/**
 可用次数视图
 */
@property (nonatomic, strong)UILabel *numberLabel;

@end

@implementation SEGVisitorAccessCodeInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_bg;

    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = kColor_W;
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 2;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).mas_offset(15.0);
        make.right.equalTo(self.contentView).mas_offset(-15.0);
        make.top.equalTo(self.contentView).mas_offset(15.0);
        make.bottom.equalTo(self.contentView).mas_offset(-10.0);
    }];
    
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_visitorspass_kong")]];
    [bgView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView).mas_offset(15);
        make.centerX.equalTo(bgView);
    }];

    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = kFont_16;
    titleLabel.textColor = kColor_1;
    titleLabel.text = @"通行证信息";
    [self.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView).mas_offset(53);
        make.centerX.equalTo(bgView);
    }];
    
    UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_visitorspass_lineleft")];
    CGFloat height = image.size.height;
    CGFloat width = image.size.width;
    UIEdgeInsets insets = UIEdgeInsetsMake(height / 2, width / 2, height / 2, width / 2);
    UIImageView *leftLine = [[UIImageView alloc] initWithImage:[[UIImage imageWithContentsOfFile:SEGPicName(@"img_visitorspass_lineleft")] resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch]];
    [bgView addSubview:leftLine];
    [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).mas_offset(15);
        make.right.equalTo(titleLabel.mas_left).mas_offset(-15);
        make.centerY.equalTo(titleLabel);
    }];
    
    UIImageView *rightLine = [[UIImageView alloc] initWithImage:[[UIImage imageWithContentsOfFile:SEGPicName(@"img_visitorspass_lineright")] resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch]];
    [bgView addSubview:rightLine];
    [rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right).mas_offset(15);
        make.right.equalTo(bgView).mas_offset(-15);
        make.centerY.equalTo(titleLabel);
    }];
    
    //二维码图片
    UIImageView *qcodeImage = [[UIImageView alloc] init];
    [bgView addSubview:qcodeImage];
    [qcodeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).mas_offset(48);
        make.centerX.equalTo(titleLabel);
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(140);
    }];
    self.qcodeImageView = qcodeImage;
    
    //提示
    UILabel *tipsLabel = [[UILabel alloc] init];
    tipsLabel.font = kFont_13;
    tipsLabel.textColor = kColor_T;
    tipsLabel.text = @"进入时，请访客出示二维码给门岗";
    [self.contentView addSubview:tipsLabel];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(qcodeImage.mas_bottom).mas_offset(28);
        make.centerX.equalTo(bgView);
    }];

    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView);
        make.right.equalTo(bgView);
        make.top.equalTo(tipsLabel.mas_bottom).mas_offset(48);
        make.height.mas_equalTo(0.5);
    }];
    
    //来访日期
    UILabel *visitDateLabel = [[UILabel alloc] init];
    visitDateLabel.font = kFont_13;
    visitDateLabel.textColor = kColor_2;
    [self.contentView addSubview:visitDateLabel];
    [visitDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).mas_offset(13);
        make.left.equalTo(bgView).mas_equalTo(15);
    }];
    self.visitDateLabel = visitDateLabel;
    
    //有效日期
    UILabel *validDateLabel = [[UILabel alloc] init];
    validDateLabel.font = kFont_13;
    validDateLabel.textColor = kColor_2;
    [self.contentView addSubview:validDateLabel];
    [validDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(visitDateLabel.mas_bottom).mas_offset(7);
        make.left.equalTo(bgView).mas_equalTo(15);
    }];
    self.validDateLabel = validDateLabel;
    
    //可用次数
    UILabel *numberLabel = [[UILabel alloc] init];
    numberLabel.font = kFont_13;
    numberLabel.textColor = kColor_2;
    [self.contentView addSubview:numberLabel];
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(validDateLabel);
        make.right.equalTo(bgView).mas_equalTo(-15);
    }];
    self.numberLabel = numberLabel;
}

- (void)updateInfoCellWithModel:(SEGVisitorAccessCodeModel *)model
{
    NSURL *qcodeUrl = [NSURL URLWithString:model.qcodeUrl];
    [self.qcodeImageView setImageWithURL:qcodeUrl placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
    self.visitDateLabel.text = [NSString stringWithFormat:@"来访日期：%@",model.vistDate];
    self.validDateLabel.text = [NSString stringWithFormat:@"有效日期：%@",model.effectiveStr];
    //根据授权门禁个数来判断是否显示可用次数
    if (model.visitorDoors.count > 0) {
        NSString *numberString = [NSString stringWithFormat:@"每个门禁可访问：%@ 次",model.allowableNum];
        NSMutableAttributedString* str1 = [[NSMutableAttributedString alloc] initWithString:numberString];
        [str1 addAttribute:NSFontAttributeName
                     value:kFont_16
                     range:NSMakeRange([str1 length] - model.allowableNum.length - 2, model.allowableNum.length)];
        self.numberLabel.attributedText = str1;
    }else {
        self.numberLabel.text = @"";
    }
   
}

@end
